using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace Madason.GenUtils
{
	public class ParameterForm : Form
	{
		TableLayoutPanel panel;
		private List<ParameterDefinition> parameterDefinition;
		private Dictionary<string, Control> parameterControls = new Dictionary<string,Control>();
		private List<ParameterDefinition> paramsMap;
		private Dictionary<string, string> arguments;

		public ParameterForm(List<ParameterDefinition> parameterDefinition, Dictionary<string, string> arguments)
		{
			this.parameterDefinition = parameterDefinition;
			this.arguments = arguments; 

			TableLayoutPanel panel2 = new TableLayoutPanel();
			panel2.Dock = DockStyle.Fill;
			

			panel = new TableLayoutPanel();
			panel.ColumnCount = 2;
			panel.Dock = DockStyle.Fill;
			panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
			panel.AutoSize = true;
			panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
			panel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
			

			Controls.Add(panel2);
			panel2.Controls.Add(panel);
			foreach (ParameterDefinition def in parameterDefinition)
			{
				Label lbl = new Label();
				lbl.Text = def.Name;
				panel.Controls.Add(lbl);

				string argument = arguments.ContainsKey(def.Name) ? arguments[def.Name] : null;

				if (def.ParameterType == ParameterType.Text)
				{
					TextBox textbox = new TextBox();
					panel.Controls.Add(textbox);
					textbox.Text = argument;
					textbox.Dock = DockStyle.Fill;
					parameterControls[def.Name] = textbox;
				}
				if (def.ParameterType == ParameterType.Bool)
				{
					CheckBox checkbox = new CheckBox();
					panel.Controls.Add(checkbox);										
					parameterControls[def.Name] = checkbox;
				}
				if (def.ParameterType == ParameterType.Directory)
				{
					FileSelector fileSelector = new FileSelector(FileSelectorMode.SelectDir);
					fileSelector.Text = argument;
					panel.Controls.Add(fileSelector);
					fileSelector.Dock = DockStyle.Fill;
					parameterControls[def.Name] = fileSelector;
				}
				if (def.ParameterType == ParameterType.File)
				{
					FileSelector fileSelector = new FileSelector(FileSelectorMode.SelectFile);
					fileSelector.Text = argument;
					panel.Controls.Add(fileSelector);
					fileSelector.Dock = DockStyle.Fill;
					parameterControls[def.Name] = fileSelector;
				}
				if (def.ParameterType == ParameterType.Selection)
				{
					ComboBox combo = new ComboBox();
					foreach (string option in def.Options)
						combo.Items.Add(option);
					panel.Controls.Add(combo);
					combo.Text = argument;
					combo.DropDownStyle = ComboBoxStyle.DropDownList;
					combo.Dock = DockStyle.Fill;
					parameterControls[def.Name] = combo;
				}
			}

			Width = 350;

			TableLayoutPanel buttonPanel = new TableLayoutPanel();
			buttonPanel.ColumnCount = 2;
			panel2.Controls.Add(buttonPanel);
			buttonPanel.Dock = DockStyle.Fill;
			buttonPanel.Height = 25;
		
			Button btnClipbord = new Button();
			btnClipbord.Text = "Copy to Clipboard";
			btnClipbord.Width = 170;
			buttonPanel.Controls.Add(btnClipbord);
			btnClipbord.Click += new EventHandler(btnClipbord_Click);
			btnClipbord.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;

			Button btnOk = new Button();
			btnOk.Text = "Ok";
			buttonPanel.Controls.Add(btnOk);
			btnOk.Click += new EventHandler(btnOk_Click);
			btnOk.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;

			Text = Process.GetCurrentProcess().ProcessName + " Parameter Help";
		}

		void btnClipbord_Click(object sender, EventArgs e)
		{
			ReadArgumentsFromControls();
			Clipboard.SetText(SimulateCommandLine());
		}

		protected void ReadArgumentsFromControls()
		{
			foreach (ParameterDefinition def in parameterDefinition)
			{
				if (def.ParameterType == ParameterType.Text)
				{
					TextBox textbox = parameterControls[def.Name] as TextBox;
					if (!String.IsNullOrEmpty(textbox.Text))
						arguments[def.Name] = textbox.Text;
				}
				if (def.ParameterType == ParameterType.Bool)
				{
					CheckBox checkbox = parameterControls[def.Name] as CheckBox;
					arguments[def.Name] = checkbox.Checked ? "yes" : "no";
				}
				if (def.ParameterType == ParameterType.Directory ||
					def.ParameterType == ParameterType.File)
				{
					FileSelector fileSelector = parameterControls[def.Name] as FileSelector;
					if (!String.IsNullOrEmpty(fileSelector.Text))
						arguments[def.Name] = fileSelector.Text;
				}
				if (def.ParameterType == ParameterType.Selection)
				{
					ComboBox combo = parameterControls[def.Name] as ComboBox;
					if (!String.IsNullOrEmpty(combo.Text))
						arguments[def.Name] = combo.Text;
				}
			}
		}


		/// <summary>
		/// ok clicked
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void btnOk_Click(object sender, EventArgs e)
		{
			ReadArgumentsFromControls();
			Console.WriteLine("your command line would have been:");
			Console.WriteLine(SimulateCommandLine());

			Close();
			
		}

		public string SimulateCommandLine()
		{
			StringBuilder buffer = new StringBuilder(Process.GetCurrentProcess().ProcessName);
			buffer.Append(" ");
			foreach (string arg in arguments.Keys)
			{
				buffer.Append(arg + "=" + arguments[arg] + " ");
			}
			return buffer.ToString();
		}

	}
}

